
<h1><span class="yiyi-st" id="yiyi-13">numpy.ma.average</span></h1>
        <blockquote>
        <p>原文：<a href="https://docs.scipy.org/doc/numpy/reference/generated/numpy.ma.average.html">https://docs.scipy.org/doc/numpy/reference/generated/numpy.ma.average.html</a></p>
        <p>译者：<a href="https://github.com/wizardforcel">飞龙</a> <a href="http://usyiyi.cn/">UsyiyiCN</a></p>
        <p>校对：（虚位以待）</p>
        </blockquote>
    
<dl class="function">
<dt id="numpy.ma.average"><span class="yiyi-st" id="yiyi-14"> <code class="descclassname">numpy.ma.</code><code class="descname">average</code><span class="sig-paren">(</span><em>a</em>, <em>axis=None</em>, <em>weights=None</em>, <em>returned=False</em><span class="sig-paren">)</span><a class="reference external" href="http://github.com/numpy/numpy/blob/v1.11.3/numpy/ma/extras.py#L465-L560"><span class="viewcode-link">[source]</span></a></span></dt>
<dd><p><span class="yiyi-st" id="yiyi-15">返回给定轴上数组的加权平均值。</span></p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name">
<col class="field-body">
<tbody valign="top">
<tr class="field-odd field"><th class="field-name"><span class="yiyi-st" id="yiyi-16">参数：</span></th><td class="field-body"><p class="first"><span class="yiyi-st" id="yiyi-17"><strong>a</strong>：array_like</span></p>
<blockquote>
<div><p><span class="yiyi-st" id="yiyi-18">要平均的数据。</span><span class="yiyi-st" id="yiyi-19">在计算中不考虑屏蔽条目。</span></p>
</div></blockquote>
<p><span class="yiyi-st" id="yiyi-20"><strong>axis</strong>：int，可选</span></p>
<blockquote>
<div><p><span class="yiyi-st" id="yiyi-21">用于平均<em class="xref py py-obj">a</em>的轴。</span><span class="yiyi-st" id="yiyi-22">如果<em class="xref py py-obj">无</em>，则对平展的数组进行平均。</span></p>
</div></blockquote>
<p><span class="yiyi-st" id="yiyi-23"><strong>权重</strong>：array_like，可选</span></p>
<blockquote>
<div><p><span class="yiyi-st" id="yiyi-24">每个元素在平均值计算中的重要性。</span><span class="yiyi-st" id="yiyi-25">权数数组可以是1-D（在这种情况下，其长度必须是沿给定轴的<em class="xref py py-obj">a</em>的大小）或与<em class="xref py py-obj">a</em>相同的形状。</span><span class="yiyi-st" id="yiyi-26">如果<code class="docutils literal"><span class="pre">weights=None</span></code>，则假设<em class="xref py py-obj">a</em>中的所有数据具有等于1的权重。</span><span class="yiyi-st" id="yiyi-27">如果<em class="xref py py-obj">权重</em>是复数，则虚部被忽略。</span></p>
</div></blockquote>
<p><span class="yiyi-st" id="yiyi-28"><strong>返回</strong>：bool，可选</span></p>
<blockquote>
<div><p><span class="yiyi-st" id="yiyi-29">Flag indicating whether a tuple <code class="docutils literal"><span class="pre">(result,</span> <span class="pre">sum</span> <span class="pre">of</span> <span class="pre">weights)</span></code> should be returned as output (True), or just the result (False). </span><span class="yiyi-st" id="yiyi-30">默认值为False。</span></p>
</div></blockquote>
</td>
</tr>
<tr class="field-even field"><th class="field-name"><span class="yiyi-st" id="yiyi-31">返回：</span></th><td class="field-body"><p class="first"><span class="yiyi-st" id="yiyi-32"><strong>average，[sum_of_weights]</strong>：（tuple of）scalar或MaskedArray</span></p>
<blockquote class="last">
<div><p><span class="yiyi-st" id="yiyi-33">沿着指定轴的平均值。</span><span class="yiyi-st" id="yiyi-34">当返回的是<em class="xref py py-obj">True</em>时，返回一个元组，其中平均值作为第一个元素，权重的和作为第二个元素。</span><span class="yiyi-st" id="yiyi-35">如果<em class="xref py py-obj">a</em>是整数类型，并且浮点小于<em class="xref py py-obj">float64</em>或输入数据类型，则返回类型为<em class="xref py py-obj">np.float64</em></span><span class="yiyi-st" id="yiyi-36">如果返回，<em class="xref py py-obj">sum_of_weights</em>始终为<em class="xref py py-obj">float64</em>。</span></p>
</div></blockquote>
</td>
</tr>
</tbody>
</table>
<p class="rubric"><span class="yiyi-st" id="yiyi-37">例子</span></p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">ma</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mf">1.</span><span class="p">,</span> <span class="mf">2.</span><span class="p">,</span> <span class="mf">3.</span><span class="p">,</span> <span class="mf">4.</span><span class="p">],</span> <span class="n">mask</span><span class="o">=</span><span class="p">[</span><span class="kc">False</span><span class="p">,</span> <span class="kc">False</span><span class="p">,</span> <span class="kc">True</span><span class="p">,</span> <span class="kc">True</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">ma</span><span class="o">.</span><span class="n">average</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">weights</span><span class="o">=</span><span class="p">[</span><span class="mi">3</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">])</span>
<span class="go">1.25</span>
</pre></div>
</div>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">ma</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mf">6.</span><span class="p">)</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="go">[[ 0.  1.]</span>
<span class="go"> [ 2.  3.]</span>
<span class="go"> [ 4.  5.]]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">avg</span><span class="p">,</span> <span class="n">sumweights</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">ma</span><span class="o">.</span><span class="n">average</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">weights</span><span class="o">=</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span>
<span class="gp">... </span>                                <span class="n">returned</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">avg</span><span class="p">)</span>
<span class="go">[2.66666666667 3.66666666667]</span>
</pre></div>
</div>
</dd></dl>
